(function ($) {
    $.fn.extend({
        serializeObject: function () {
            var o = {};
            var a = this.serializeArray();
            $.each(a, function () {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value || '');
                } else {
                    o[this.name] = this.value || '';
                }
            });
            return o;
        }
    });

})(jQuery);

function loadPageByIdAndUri(id, uri) {
    $("#" + id).load(uri);

}

function loadMainBody(uri) {
    loadPageByIdAndUri('main', uri);
}

function loadPage(id) {
    loadPageByIdAndUri(id, id);
}

function commentBlog() {
    var blogId = $("#blogId").val();
    var comment = ckEditor.document.getBody().getHtml();
    console.log(blogId);
    console.log(comment);
}

function addCommentField() {
    var li = $("#commentFiled li:last-child");
    li.clone().insertBefore(li);
}

function saveAdmin() {
    if (!checkAccountIsExist()) {
        var regJson = $('form').serializeObject();
        regJson["password"] = hex_md5(regJson["password"]);
        $.ajax({
            type: "POST",
            url: "/admin/save",
            data: regJson,
            success: function () {
                loadMainBody('list');
            }
        });
    }
}

/**
 * 判断帐号是否存在
 */
function checkAccountIsExist() {
    var id = $("#adminId").val();
    var account = $("#account").val();
    var url = "/admin/exist?account=" + account + "&id=" + id;
    var isExist = false;
    $.ajax({
        url: url,
        async: false,
        success: function (msg) {
            var display = msg ? "inline" : "none";
            $(".span-exist-tip").css("display", display);
            isExist = msg;
        }
    });
    return isExist;
}

function saveBlog() {
    var form = new FormData(document.getElementById('add_blog_form'));
    form.append("file", document.getElementById('doc-form-file').files[0]);
    form.append('content', ckEditor.document.getBody().getHtml());
    $.ajax({
        type: "POST",
        url: "/blog/save",
        data: form,
        processData: false,
        contentType: false,
        success: function () {
            loadMainBody('/admin/blog/list');
        }

    });
}

/*function saveBlog() {
    $("#add_blog_form").ajaxSubmit();
}*/


/*function addComment() {
    $("#comment_form").ajaxSubmit();
    var blogId = $("#blogId").val();
    loadMainBody('detail/' + blogId);
}*/
function addComment() {
    var form = new FormData(document.getElementById('comment_form'));
    $.ajax({
        type: "POST",
        url: "/comment/add",
        data: form,
        processData: false,
        contentType: false,
        success: function () {
            var blogId = $("#blogId").val();
            loadMainBody('detail/' + blogId);
        }

    });
}

/**
 * 删除指定数据
 * @param uri 要删除的uri地址
 * @param successUri 删除成功回调地址
 */
function deleteObj(uri, successUri) {
    $.ajax({
        type: "delete",
        url: uri,
        success: function () {
            loadMainBody(successUri);
        }
    });
}

function deleteAllObj(uri, successUri, classId) {
    var idArr = new Array();
    var idStr = "";
    $("." + classId).each(function () {
        if ($(this).prop("checked")) {
            var id = $(this).attr("objId");
            idArr.push(id);
            idStr += "id=" + id + "&";
        }
    })

    if (idStr.length > 0) {
        idStr = idStr.substr(0, idStr.lastIndexOf("&"));
        $.ajax({
            type: "delete",
            url: uri + "?" + idStr,
            dataType: 'text',
            success: function () {
                loadMainBody(successUri);
            }
        });

    }

}

/**
 * 使选择框的选中状态与父选择框一致
 * @param id
 * @param childClass
 */
function setCheckBox(id, childClass) {
    var isChecked = $("#" + id).prop("checked");
    $("." + childClass).prop("checked", isChecked);
}

/**
 * 退出后台
 */
function logoutBackground() {
    $.get("/admin/logout", function (msg) {
        if (msg === "true") {
            window.location.href = "/admin/login/form";
        } else {
            alert("退出异常!");
        }
    });
}

/**
 * 清空未读评论数
 */
function clearUnreadComments() {
    $.ajax({
        url: "/comment/unread/clear",
        type: "delete",
        success: function (result) {
            if (result) {
                $(".unread-comment").text(0);
            }
        }
    });
}
